--
-- Author: yawei.zhu
-- Date: 2018-06-22 17:14:26
--
local NewTeamMemberPanel = require("app.team.views.NewTeamMemberPanel")
local PvpChampionSeek = class("PvpChampionSeek")

function PvpChampionSeek:ctor(node)
	self._seekNode = node

	self:initNode()
end

function PvpChampionSeek:initNode()
	local seekPanel = self._seekNode:getChildByName("Panel")
	local titleTxt = seekPanel:findChild("Panel_tlt/tlt")
	titleTxt:setString( L("champion_team1") )

	self._tipsPanel = seekPanel:getChildByName("tips")
	local txtNotice = self._tipsPanel:getChildByName("text")
	txtNotice:setString(L("champion_team3"))

	-- 上部
	self._topPanel = seekPanel:getChildByName("Panel_top")
	-- self._selectBtn = self._topPanel:getChildByName("selected_btn")
	self._iconImage = self._topPanel:getChildByName("icon")
	txtNotice = self._topPanel:getChildByName("Text")
	txtNotice:setString( L("champion_team4") )
	local txtDesc1 = self._topPanel:getChildByName("tlt")
	txtDesc1:setString( L("champion_team5") )
	local txtDesc2 = self._topPanel:getChildByName("num_tlt")
	txtDesc2:setString("")
	local txtDesc3 = self._topPanel:getChildByName("selected_tlt")
	txtDesc3:setString("")
	local selectBtn = self._topPanel:getChildByName("selected_btn")
	selectBtn:setVisible(false)
	local tipsTime = self._topPanel:getChildByName("tips_time")
	tipsTime:setVisible(false)
	--TODO 设置图标


	-- 底部
	local bomPanel = seekPanel:getChildByName("Panel_bom")
	self._createBtn = bomPanel:getChildByName("Button_enter")
	self._createBtnName = self._createBtn:getChildByName("name")
	self._bomText = bomPanel:getChildByName("Text")
	self._bomNotice = bomPanel:findChild("Panel_tips/tips_text")
	self._bomIconBg = bomPanel:findChild("Panel_tips/icon")
	self._singleBtn = bomPanel:getChildByName("Button_found")
	self._singleBtn:getChildByName("name"):setString( L("champion_team6") )
	self._singleBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "PvpChampion_register")
	end)
	self._bomText:setString("")
	self._bomNotice:setString(L("challengeTips5"))
	self._bomIconBg:setPositionX(self._bomNotice:getPositionX() + self._bomNotice:getContentSize().width)

	self._createBtn:addClickEventListener(function() -- 创建队伍/开始战斗 按钮响应
		if L("champion_team7") == self._createBtnName:getString() then
			executeFunc(self._onOperate, "PvpChampion_createTeam" )
		elseif L("champion_register6") == self._createBtnName:getString() then
			if self._isLeader then
				executeFunc(self._onOperate, "PvpChampion_register")
			else
				display.pushToast( L("champion_register7") )
			end
		end
	end)

	local searchNode = bomPanel:getChildByName("Button_tips")
	-- local timeline = searchNode:getTimeline()
	searchNode:getChildByName("Panel_1"):addClickEventListener(function()
		executeFunc(self._onOperate, "PvpChampion_search_1")
	end)

	-- 推荐
	local recommendPanel = seekPanel:getChildByName("Panel_tuijian")
	recommendPanel:setVisible(false)

	-- 队伍
	self._teamPanel = seekPanel:getChildByName("Panel_team")
	self._teamMemberPanel = NewTeamMemberPanel.create( self._teamPanel:getChildByName("player") )
	self._teamMemberPanel:registerOnOperate(function(op)
		executeFunc(self._onOperate, op)
	end)

	-- 按钮相关
	local dataPanel = self._teamPanel:getChildByName("Panel_data")
	self._btnApply = dataPanel:getChildByName("Button_apply")
	self._applyDot = self._btnApply:getChildByName("dotNew")
	dataPanel:getChildByName("Button_legion"):setVisible(false)
	dataPanel:getChildByName("Button_team"):setVisible(false)
	self._btnLeave = dataPanel:getChildByName("Button_leave")
	self._btnSet = dataPanel:getChildByName("Button_set")
	self._panelNum = dataPanel:getChildByName("Panel_num")
	self._totalPow = self._panelNum:getChildByName("combat_num")
	self._powDesc = self._panelNum:getChildByName("combat_tlt")
	self._powDesc:setString( L("champion_team8") )

	self._btnApply:addClickEventListener(function() -- 展开申请列表
		executeFunc(self._onOperate, "PvpChampion_showApplyList")
		self:_refreshNewApplyerFlag( false )
	end)
	self._btnLeave:addClickEventListener(function() -- 退出队伍
		local creataeTeamDialog = display.newConfirmDialog( L("champion_team9"),
			function() -- 确定
				executeFunc(self._onOperate, "PvpChampion_quitTeam")
			end)
		UIHandler:addPopDialog( creataeTeamDialog )
	end)
	self._btnSet:addClickEventListener(function() -- 打开己方队伍设置
		executeFunc(self._onOperate, "PvpChampion_TeamSettingUI", { 2 } ) -- "我"的设置
	end)
end

function PvpChampionSeek:tick(endTime)
	local remainedTime = endTime - app.session:getServerTime()
	if remainedTime < 0 then remainedTime = 0 end
	local hour = math.floor(remainedTime / 3600)
	local min = math.floor((remainedTime - hour * 3600) / 60 )
	local sec = math.floor(remainedTime % 60)
	self._bomText:setString( string.format( L("champion_register5"), hour, min, sec ) )
end

function PvpChampionSeek:_refreshTeamMemberList(team, isNewApplyerFlag)

	self._teamPanel:setVisible(false)
	self._tipsPanel:setVisible(false)
	self._singleBtn:setVisible(true)
	-- self._createBtn:setVisible(true)

	if team and team:isInTeam(UD.player.uid) then
		self._createBtnName:setString( L("champion_register6") )
		self._teamPanel:setVisible(true)
		self._teamMemberPanel:setData( team )
		self._singleBtn:setVisible(false)

		self._isLeader = team:isLeader(UD.player.uid)

		-- 玩法最大人数
		local maxNum = 3

		-- 刷新超限制人数禁止标志
		self._teamMemberPanel:refreshForbiddenIconStatus( maxNum )

		-- 队伍真实玩家的战斗力
		local teamCombatPower = team:getTeamTotalCapacity()
		self._totalPow:setString( teamCombatPower )
		-- 居中对齐
		display.setNodesMiddle(self._panelNum, nil, self._powDesc, self._totalPow)
		
		self:_refreshNewApplyerFlag( isNewApplyerFlag )
	
		self:updateTeamBtn( team:isLeader(UD.player.uid) )

	else
		self._createBtnName:setString( L("champion_team7") )
		self._tipsPanel:setVisible(true)
	end
end

-- 刷新新申请者new标记状态
function PvpChampionSeek:_refreshNewApplyerFlag( visible )
	self._applyDot:setVisible( visible )
end

-- 更新队伍按钮状态
function PvpChampionSeek:updateTeamBtn(isLeader)
	if isLeader then
		self._btnApply:setVisible(true)
	else
		self._btnApply:setVisible(false)
	end
end

function PvpChampionSeek:setShow(visible)
	self._seekNode:setVisible(visible)
end

function PvpChampionSeek:isVisible()
	return self._seekNode:isVisible()
end


function PvpChampionSeek:onOperate(cb)
	self._onOperate = cb
end

return PvpChampionSeek

